<?php
namespace App\Service\OrderService\OrderInterfaceService;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\PssModel\OrderRefund;
use App\Model\PssModel\OrderRefundLog;

/**
 * BBC售后数据状态同步
 */
class OrderAfterSaleVerifySuccessLater extends BaseService
{
    /**
     * 订单售后处理
     */
    public function index( array $param = [] )
    {
        $binfo  = $param['binfo'];
        $bid    = $binfo['bid'];
        $requestData = $param['requestData'];
        if( empty($requestData['refund_id']) ) {
            return Normal::returnCode(Normal::FAIL,"售后单号refund_id不能为空");
        }
        if( empty($requestData['order_sn']) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单号order_sn不能为空");
        }
        
        if( !isset($requestData['refund_status']) ) {
            return Normal::returnCode(Normal::FAIL,"售后状态refund_status不能为空");
        }
        
        if( !isset($requestData['refund_remark']) ) {
            return Normal::returnCode(Normal::FAIL,"售后备注refund_remark不能为空");
        }
        
        if( empty($requestData['create_user_id']) ) {
            return Normal::returnCode(Normal::FAIL,"create_user_id不能为空");
        }
        
        if( empty($requestData['create_user_name']) ) {
            return Normal::returnCode(Normal::FAIL,"create_user_name不能为空");
        }
        
        $orderRefund = OrderRefund::instance()->info(['id'=>$requestData['refund_id'],'bid'=>$bid,'order_sn'=>$requestData['order_sn']]);
        if( empty($orderRefund)) {
            return Normal::returnCode(Normal::FAIL,"售后申请订单记录不存在，请联系管理员进行核实处理");
        }
        
        $refund_status = $requestData['refund_status'];
        $refund_remark = $requestData['refund_remark'];
        if( !isset(OrderRefund::instance()->refund_status[$refund_status]) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单状态未定义:{$refund_status}，请联系管理员进行核实处理");
        }
        
        $order_refund_id = $orderRefund['id']; 
        try
        {
            OrderRefund::instance()->startTrans();
            //修改售后订单状态
            $result = OrderRefund::instance()->where('id',$order_refund_id)->where('refund_status','>',1)->update([
                'refund_status' => $refund_status,
                'refund_remark' => $refund_remark,
                'md'            => time(),
            ]);
            if( !$result ) {
                return Normal::returnCode(Normal::FAIL,"售后流程消息处理失败，请联系管理员进行核实处理");
            }
            
            //写入订单日志
            $contents = "订单发起：". OrderRefund::instance()->refund_status[$refund_status] . ", 备注：{$requestData['refund_remark']}";
            OrderRefundLog::instance()->addLog($order_refund_id, $contents, $requestData['create_user_id'], $requestData['create_user_name']);
            OrderRefund::instance()->commit();
            return Normal::returnCode(Normal::SUC,"售后流程消息处理完成");
        } 
        catch ( \Throwable $th) 
        {
            OrderRefund::instance()->rollback();
            return Normal::returnCode(Normal::FAIL,"售后订单状态未定义:{$refund_status}，请联系管理员进行核实处理");
        }
    }
}